package com.extremeforce.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.extremeforce.R;
import com.extremeforce.function.GetNetImage;
import com.extremeforce.module.DetailInfoActivity;
import com.extremeforce.module.GetCouponActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CouponListAdapter extends BaseAdapter{
	
	private ArrayList<HashMap<String,Object>> list;
	private Context context ;
	public CouponListAdapter(ArrayList<HashMap<String,Object>> list,Context context){
		this.list = list;
		this.context = context;		
	}
	

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater mInflater = LayoutInflater.from(context);
		
		final HashMap<String,Object> map = list.get(position);
		
		convertView = mInflater.inflate(R.layout.couponitem, null);
		
		TextView titleTextView = (TextView)convertView.findViewById(R.id.couponTitle);
		
		titleTextView.setText(String.valueOf(map.get("title")));
		
		TextView descTextView = (TextView)convertView.findViewById(R.id.couponDesc);
		descTextView.setText(String.valueOf(map.get("desc")));
		
		ImageView couponImageView = (ImageView)convertView.findViewById(R.id.couponImage);
		couponImageView.setImageBitmap((Bitmap)(map.get("image")));
		
		TextView idTextView = (TextView)convertView.findViewById(R.id.couponid);
		idTextView.setText(String.valueOf(map.get("id")));
		
		
		
		
		convertView.setOnClickListener(
				new View.OnClickListener() {
					
					public void onClick(View v) {
						Bitmap bitmap = (Bitmap)map.get("image");
						byte[] array = GetNetImage.imageToBytes(bitmap);
						
						Bundle bundle = new Bundle();
						bundle.putString("title",String.valueOf(map.get("title")));
						bundle.putString("desc",String.valueOf(map.get("desc")));
						bundle.putByteArray("image",array);
						bundle.putString("num",String.valueOf(map.get("num")));
						bundle.putString("coupon_id",String.valueOf(map.get("id")));
						
						Intent intent = new Intent();
						intent.setClass(context,GetCouponActivity.class);
						intent.putExtras(bundle);
						context.startActivity(intent);
					}
				}
				);
		
		
		return convertView;
	}
	
	

}
